<template>
  <div>

    <div class="headBar">
      <el-form inline>
        <el-form-item>
          <el-input v-model="name" placeholder="请输入名字" suffix-icon="el-icon-search" style="width: 200px;"
                    @keyup.enter.native="loadData"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="loadData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-refresh" @click="resetParam">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus"  @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tableData">
      <el-table :data="tableData"
                :header-cell-style="{ background: '#f2f5fc', color: '#555555' }"
                border
                :row-class-name="tableRowClassName"
      >
        <el-table-column label="ID" width="60">
          <template v-slot="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="account" label="账号" width="180"></el-table-column>
        <!--      <el-table-column prop="password" label="密码" width="180">-->
        <!--        <template v-slot="scope">-->
        <!--          <div>-->
        <!--            <span v-if="viewPwdRow !== scope.$index">***</span>-->
        <!--            <span v-else>{{ scope.row.password }}</span>-->
        <!--            <i class="el-icon-view" style="margin-left: 60px" @click="togglePassword(scope.$index)"></i>-->
        <!--          </div>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column prop="name" label="姓名" width="180"></el-table-column>
        <el-table-column prop="dept" label="所属部门" width="180"></el-table-column>
        <el-table-column prop="statue" label="状态" width="120">
          <template v-slot:default="scope">
            <el-tag
              :type=" scope.row.statue === '0' ? 'danger' : (scope.row.statue === '1' ? 'success' : 'warning') "
              disable-transitions>{{ scope.row.statue === '1' ? '正常' : (scope.row.statue === '0' ? '冻结' : '管理员') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operate" label="操作">
          <template v-slot="scope">
            <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
            <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row)"
              style="margin-left: 5px;"
            >
              <el-button slot="reference" size="small" type="danger" >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>


    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-col :span="20">
            <el-input v-model="form.account"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-col :span="20">
            <el-input v-model="form.password"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="statue">
          <el-radio-group v-model="form.statue">
            <el-radio label="1">正常</el-radio>
            <el-radio label="0">冻结</el-radio>
            <el-radio label="2">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <el-col :span="20">
            <el-input v-model="form.dept"></el-input>
          </el-col>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="提示"
      :visible.sync="DialogUpdate"
      width="30%"
      center>
      <el-form ref="form" :rules="rules2" :model="form" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-col :span="20">
            <el-input v-model="form.account"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-col :span="20">
            <el-input v-model="form.password" placeholder="若要重置密码请填写"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="statue">
          <el-radio-group v-model="form.statue">
            <el-radio label="1">正常</el-radio>
            <el-radio label="0">冻结</el-radio>
            <el-radio label="2">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <el-col :span="20">
            <el-input v-model="form.dept"></el-input>
          </el-col>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="DialogUpdate = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>
  </div>

</template>
<script>
export default {
  name: "UserManage",
  data() {
    let checkDuplicate =(rule,value,callback)=>{
      if(this.form.id){
        return callback();
      }
      this.$axios.post(this.$httpUrlUser+"/findByAccount", this.form).then(res=>res.data).then(res=>{
        if(res.code !==200){
          callback()
        }else{
          callback(new Error('账号已经存在'));
        }
      })
    };

    return {
      DialogUpdate:false,
      viewPwdRow: null,
      name:'',
      showPassword:false,
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      centerDialogVisible:false,
      form:{
        id:'',
        account:'',
        password:'',
        name:'',
        statue:'',
        dept:''
      },
      rules: {
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'},
          {validator:checkDuplicate,trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名字', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        dept: [
          {required: true, message: '请输入部门', trigger: 'blur'}
        ],
        statue: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        phone: [
          {required: true,message: "手机号不能为空",trigger: "blur"},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}
        ]
      },
      rules2: {
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'},
          {validator:checkDuplicate,trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名字', trigger: 'blur'}
        ],
        dept: [
          {required: true, message: '请输入部门', trigger: 'blur'}
        ],
        statue: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        phone: [
          {required: true,message: "手机号不能为空",trigger: "blur"},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}
        ]
      }
    }
  },
  methods:{
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 === 1) {
        return 'warning-row';
      }
    },
    togglePassword(index) {
      if (this.viewPwdRow === index) {
        this.viewPwdRow = null;
      } else {
        this.viewPwdRow = index;
      }
    },

    del(row){
      this.$axios.post(this.$httpUrl+'/user/delete', row).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code === 200){
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadData()
        }else{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }
      })
    },
    mod(row){
      this.form.id = row.id
      this.form.account = row.account
      this.form.name = row.name
      this.form.password = ''
      this.form.statue = row.statue
      this.form.dept = row.dept

      this.DialogUpdate = true
      //赋值到表单
      this.loadData()
    },
    add(){
      this.form = {
        id:'',
        account:'',
        password:'',
        name:'',
        statue: '1',
        dept:''
      }
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate();
      })
      this.loadData()
    },
    save(){
      if(this.form.id){
        this.doMod();
      }else{
        this.doAdd();
      }
    },
    doAdd(){
      console.log(this.form)
      this.$refs['form'].validate((valid) => {
        console.log(this.form+'validate')
        if (valid) {
          this.$axios.post(this.$httpUrl+'/user/add',this.form).then(res=>res.data).then(res=>{
            console.log(res)
            if(res.code === 200){
              this.$message({
                message: '操作成功！',
                type: 'success'
              });
              this.centerDialogVisible = false
              this.loadData()
            }else{
              this.$message({
                message: res.msg,
                type: 'error'
              });
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/user/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code === 200){

          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.DialogUpdate = false
          this.loadData()
          this.resetForm()
        }else{
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }

      })
    },
    resetParam(){
      this.name=''
    },
    loadData(){
      this.$axios.post(this.$httpUrl+'/user/getByPage',{
        pageSize:this.pageSize,
        pageNow:this.pageNum,
        id:'',
        account:'',
        name:this.name,
        statue:'',
        dept:''
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code === 200){
          this.tableData=res.data.rowData
          this.total=res.data.totalRows
        }else{
          alert('获取数据失败')
        }

      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadData()
    },
  },
  beforeMount() {
    this.form = {
      id:'',
      account:'',
      password:'',
      name:'',
      statue: '',
      dept:''
    }
    this.name = '';
    this.loadData()
  }
}
</script>

<style scoped>
.headBar {
  padding: 12px 0px 0px 18px;
  border-radius: 0 0 15px 15px;
  box-shadow: 0 2px 8px 2px rgba(0, 0, 0, 0.1);
}

.tableData {
  margin: 10px;
}
</style>





